package yxy.game.pm2.module.shop;

import org.redisson.api.RBucket;
import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.shop;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;

import java.util.ArrayList;

public abstract class ShopModule {
	// bean===========================================================================

	private String playerCode;
	private ArrayList<Goods> goods = new ArrayList<>();
	private String dataMD5;// 数据文件的MD5

	public String getPlayerCode() {
		return playerCode;
	}

	public void setPlayerCode(String playerCode) {
		this.playerCode = playerCode;
	}

	public ArrayList<Goods> getGoods() {
		return goods;
	}

	public void setGoods(ArrayList<Goods> goods) {
		this.goods = goods;
	}

	public String getDataMD5() {
		return dataMD5;
	}

	public void setDataMD5(String dataMD5) {
		this.dataMD5 = dataMD5;
	}

	// abstract===========================================================================
	abstract public void set();

	abstract protected void shuffleGoods();

	public void checkMD5(String md5) {
		if (getDataMD5() != null && getDataMD5().equals(md5)) {
			return;
		}
		setDataMD5(md5);
		shuffleGoods();
		set();
	}

	// func===========================================================================
	public static shop data(int id) {
		return DataCenter.getData(id, shop.class);
	}

	// redis==========================================================================

	static public String key() {
		return "PLAYER:%s:SHOP:%s";
	}

	static private RedisConnection redis() {
		return RedisManager.getInstance().getConnection(RedisType.REDIS_GAME);
	}

	static protected  <V> RBucket<V> touch(String key) throws RedisException {
		return RedisManager.client(redis()).getBucket(key);
	}

	public static <V extends ShopModule> V getv(String key) throws RedisException {
		RBucket<V> bucket = touch(key);
		return bucket.get();
	}

	public static <V extends ShopModule> void setv(String key, V m) throws RedisException {
		RBucket<V> bucket = touch(key);
		bucket.set(m);
	}
}
